package logic.service;

import dao.CalendarDAO;
import dao.StudentDAO;
import entity.Calendar;
import entity.Student;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import vo.IValueObject;
import vo.StudentVO;

/**
 *
 * @author Alexander
 */
public class StudentService implements IService {

    @Override
    public void persist(IValueObject vo, EntityManager em) {
        Student entity = new Student();
        entity.setUsername(((StudentVO) vo).getUsername());
        entity.setName(((StudentVO) vo).getName());
        entity.setLastName(((StudentVO) vo).getLastName());
        entity.setPassword(((StudentVO) vo).getPassword());
        entity.setCareer(((StudentVO) vo).getCareer());
        entity.setBirthDate(((StudentVO) vo).getBirthDate());
        entity.setActiveUser(((StudentVO) vo).getActiveUser());
        entity.setAdministrator(((StudentVO) vo).isAdministrator());
        Calendar cal = new Calendar();
        cal.setCreatedSchedule(false);
        cal.setStudent(entity);
        new CalendarDAO().persist(cal, em);
        cal = (Calendar) new CalendarDAO().findByUser(entity, em);
        entity.setCalendar(cal);

        //TODO
        //entity.setWallet();

        new StudentDAO().persist(entity, em);
    }

    @Override
    public IValueObject find(Object id, EntityManager em) {
        Student a = (Student) new StudentDAO().find(id, em);
        if (a != null) {
            return a.toVo();
        } else {
            return null;
        }
    }

    @Override
    public void update(IValueObject vo, EntityManager em) {
        Student entity = new Student();
        entity.setUsername(((StudentVO) vo).getUsername());
        entity.setName(((StudentVO) vo).getName());
        entity.setLastName(((StudentVO) vo).getLastName());
        entity.setPassword(((StudentVO) vo).getPassword());
        entity.setCareer(((StudentVO) vo).getCareer());
        entity.setBirthDate(((StudentVO) vo).getBirthDate());
        entity.setActiveUser(((StudentVO) vo).getActiveUser());
        entity.setAdministrator(((StudentVO) vo).isAdministrator());
        Calendar calendar = (Calendar) new CalendarDAO().find(((StudentVO) vo).getCalendarId(), em);
        entity.setCalendar(calendar);

        new StudentDAO().update(entity, em);

    }

    public StudentVO login(StudentVO vo, EntityManager em) {
        Student entity = new Student();
        entity.setUsername(vo.getUsername());
        entity.setPassword(vo.getPassword());

        Student alumno = new StudentDAO().login(entity, em);

        if (alumno != null) {
            return alumno.toVo();
        } else {
            return null;
        }
    }

    @Override
    public void delete(Object id, EntityManager em) {
        new StudentDAO().delete(id, em);
    }

    /**
     * Ejecuta la operacion de busqueda del usuario basandose en la IP
     *
     * @param ip Ip del usuario buscado
     * @param em Comtrolador de transacciones
     * @return El usuario buscado
     */
    public StudentVO findByIp(Object ip, EntityManager em) {
        Student student = new StudentDAO().findByIP(ip, em);

        if (student != null) {
            return student.toVo();
        } else {
            return null;
        }
    }

    @Override
    public List<IValueObject> getList(EntityManager em) {
        List<IValueObject> studentVO = new ArrayList<>();
        for (Object student : new StudentDAO().getList(em)) {
            studentVO.add(((Student) student).toVo());
        }
        return studentVO;
    }
}
